package ro.gateway.aida.charts;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import ro.gateway.aida.search.ResultCell;
import ro.gateway.aida.search.ResultListCell;
import ro.gateway.aida.search.ResultRow;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jun 20, 2004
 * Time: 12:20:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChartUtils {
	/**
	 * @param items
	 * @param column
	 * @param language
	 * @param date_to_year daca e true extrag doar anul din data din coloana respectiva
	 * @return
	 */
	public static ArrayList getDistinctValues(
		  List items,
		  int column,
		  String language,
		  boolean date_to_year,
		  ChartWizzard$ActionForm form) {
		ArrayList result = new ArrayList();
		// caut categorii distincte
		Iterator it = form.search_results.iterator();
		while (it.hasNext()) {
			Object orow = (Object) it.next();
			if (!(orow instanceof ResultRow)) {
				System.err.println("ChartWizzard$ActionForm.populate_category_dataset: expected row!");
				continue;
			}
			ResultRow row = (ResultRow) orow;
			row.setViewLang(language);

			Object ocell = row.get(column);
			String val = null;
			if (ocell instanceof ResultListCell) {
				ResultListCell listcell = (ResultListCell) ocell;
				Iterator subcells = listcell.getItems().iterator();
				while (subcells.hasNext()) {
					ResultCell cell = (ResultCell) subcells.next();
					val = cell.toString();
				}
			} else if (ocell instanceof ResultCell) {
				ResultCell cell = (ResultCell) ocell;
				val = cell.toString();
			} else {
				System.err.println("ChartWizzard$ActionForm.getDistinctValues: expected cell!");
				continue;
			}
			if (date_to_year) {
				int idx = val.indexOf("-");
				if (idx > 0) {
					val = val.substring(0, idx);
				}
			}
			if (val != null && !result.contains(val)) {
				result.add(val);
			}
		}
		Collections.sort(result);
		return result;
	}

	static Number compute_chart_value(Object category,
						  Object group,
						  ChartWizzard$ActionForm form) {
		if (form.search_results == null) {
			return new Integer(0);
		}

		Number result = new Integer(0);
		if ("prjcounter".equals(form.y_axe_criteria)) {
			Iterator it = form.search_results.iterator();
			int rez = 0;
			while (it.hasNext()) {
				ResultRow row = (ResultRow) it.next();
				if (rowmatch(row, category, group, 1, 2, form)) {
					rez++;
				}
			}
			result = new Integer(rez);
		} else {
			Iterator it = form.search_results.iterator();
			long rez = 0;
			while (it.hasNext()) {
				ResultRow row = (ResultRow) it.next();
				if (rowmatch(row, category, group, 1, 2, form)) {
					ResultCell cell_amount = (ResultCell) row.get(0);
					String string_amount = cell_amount.toString();
					int space_idx = string_amount.indexOf(" ");
					if (!string_amount.equals("--") && (space_idx != -1)) {
						try {
							NumberFormat nf = NumberFormat.getInstance();
							double ival = nf.parse(string_amount.substring(0, space_idx)).doubleValue();
							rez += ival;
						} catch (Throwable th) {
							th.printStackTrace();
						}
					}
				}
			}
			result = new Long(rez);
		}
		return result;
	}
	static boolean rowmatch(ResultRow row,
				  Object category, Object group,
				  int cat_idx, int gr_idx,
				  ChartWizzard$ActionForm form) {
		boolean category_match = false;

		ResultCell cell = (ResultCell) row.get(cat_idx);
		if (cell instanceof ResultListCell) {
			ResultListCell listcell = (ResultListCell) cell;
			Iterator it = listcell.getItems().iterator();
			while (it.hasNext()) {
				ResultCell icell = (ResultCell) it.next();
				if (icell.toString().equals(category)) {
					category_match = true;
				} else if (form.x_axe_criteria.startsWith("year#")) {
					if (icell.toString().startsWith(category.toString())) {
						category_match = true;
					}
				}
			}
		} else {
			if (cell.toString().equals(category)) {
				category_match = true;
			} else if (form.x_axe_criteria.startsWith("year#")) {
				if (cell.toString().startsWith(category.toString())) {
					category_match = true;
				}
			}
		}

		if (group == null || category_match == false) {
			return category_match;
		}

		//
		boolean group_match = false;
		cell = (ResultCell) row.get(gr_idx);
		if (cell instanceof ResultListCell) {
			ResultListCell listcell = (ResultListCell) cell;
			Iterator it = listcell.getItems().iterator();
			while (it.hasNext()) {
				ResultCell icell = (ResultCell) it.next();
				if (icell.toString().equals(group)) {
					group_match = true;
				} else if (form.x_axe_group_criteria.startsWith("year#")) {
					if (icell.toString().startsWith(group.toString())) {
						group_match = true;
					}
				}
			}
		} else {
			if (cell.toString().equals(group)) {
				group_match = true;
			} else if (form.x_axe_group_criteria.startsWith("year#")) {
				if (cell.toString().startsWith(group.toString())) {
					group_match = true;
				}
			}
		}

//		if (category_match && group_match) {
//			System.out.println("rowmatch:" + row + " == " + category + "," + group);
//		}
		return category_match && group_match;
	}

}
